<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Calculator</title>
    <style type="text/css">
        html { font-family: "Helvetica", sans-serif; color: #444; background-color: #eee; }
        body { width: 600px; margin: 20px auto; }
        h1 { text-align: center; color: #0087af; text-shadow: 0px 1px 0px #fff; }
        div#footer { padding: 10px; text-align: center; font-size: 80%; font-weight: bold; text-transform: uppercase; }
        div#body { padding: 20px; border-radius: 10px; box-shadow: 0px 1px 4px #ccc; background-color: #fff; }
        form { text-align: center; }
        input#in { width: 460px; }
        h2 { text-align: center; padding: 5px; border-top: 1px dotted #ccc; border-bottom: 1px dotted #ccc; }
        em { color: #666; }
    </style>
</head>
<body>

<h1>CS143 Calculatinator</h1>

<div id="body">
    <form method="GET">
        <input type="text" name="expr" id="in"><input type="submit" value="Calculate">
    </form>

    <ul>
        <li>Only numbers and +,-,* and / operators are allowed in the expression.
        <li>The evaluation follows the standard operator precedence.
        <li>The calculator does not support parentheses.
    </ul>

    <?php
    if ($_GET['expr']) {
        print "<h2>Result</h2>\n";
        $pattern = "/^[+-]?[\d]+(\.[\d]*)? *(([\/*] *[-+]?|\+ +\+|- +-|\+ *-|- *\+|[+-] *)[\d]+(\.[\d]*)? *)*$/";
        if (preg_match($pattern, $_GET['expr']) < 1) {
            print "Invalid input expression " . $_GET['expr'] . ".\n";
        }
        else {
            $str = "";
            $expr = '$str = ' . $_GET['expr'] . ';';
            @eval($expr);
            echo "<em>" , $_GET['expr'] , "</em> = <strong>" , $str , "</strong>";
        }
    }
    ?>
</div>

<div id="footer">
    Steven La &mdash; ver 1.0 (April 4, 2012)
</div>

</body>
</html>
